package com.menchao.carsales.controller;

import com.menchao.carsales.common.http.Const;
import com.menchao.carsales.common.http.ResponseCode;
import com.menchao.carsales.common.http.ResponseData;
import com.menchao.carsales.dao.EmployeeMapper;
import com.menchao.carsales.pojo.Employee;
import com.menchao.carsales.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.function.ServerResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Porject: carsales
 * Date: 2021/7/13
 * Author: menchao
 * Description:
 */

@RestController
@RequestMapping("user")
public class UserCenterController {

    @Autowired
    private IUserService userService;



    /**
     * 接收JSON格式
     * @param body
     * @param headers
     * @param session
     * @return
     */
    @RequestMapping(value = "login", consumes = "application/json")
    public ResponseData login(@RequestBody Map<String, Object> body,
                              @RequestHeader HttpHeaders headers,
                              HttpSession session) {
        String employeeId =  (String)body.get("employeeId");
        String password =  (String)body.get("password");
        if (employeeId == null || password == null) {
            System.out.println("employeeId :" + employeeId + ",password: " + password);
            return ResponseData.errorMessageForEmptyUserOrPassword("用户名或密码为空");
        }
        ResponseData response = userService.login(Integer.valueOf(employeeId), password);
        if (response.isSuccess()) {
            session.setAttribute(Const.CURRENT_USER, response.getData());
            Map<String, Object> map = new HashMap<>(1);
            map.put("token", session.getId());
            //返回用户信息
            Employee employee = (Employee)response.getData();
            employee.setPassword("");
            map.put("userInfo",employee);
            response = ResponseData.success(map);
        }
        System.out.println("userId: " + employeeId + ",password: " + password + ",data: " + response.toString());
        return response;
    }

    //获取雇员信息
    @RequestMapping(value = "info")
    public ResponseData<Employee> userInfo(HttpSession session){

        Employee employee = (Employee) session.getAttribute(Const.CURRENT_USER);
        if (employee == null) {
            return ResponseData.errorMessage("用户未登录，无法获取当前用户信息");
        }
        System.out.println("info sessionid: " + session.getId() + "，employee: " + employee.toString());
        return ResponseData.success(employee);
    }

    //登出
    @RequestMapping(value = "logout")
    public ResponseData logout(HttpSession session) {
        System.out.println("logout sessionid: " + session.getId());
        session.removeAttribute(Const.CURRENT_USER);
        return ResponseData.success(null);
    }

}




//       //log.info("userId:{}, password:{}, data:{}", employeeId, password, response.getData());